<script setup lang="ts">
import { message } from 'ant-design-vue'
import IconLogo from '@/assets/icons/IconLogo.vue'
import { register, getCode } from '@/api/user'
import { onMounted, ref, reactive } from 'vue'
import { navigateTo } from '#app'
// 验证码图片
const captchaUrl = ref<string>('')
// 表单数据
const formState = reactive({
  mobile: '',
  nickname: '',
  captchaCode: '',
})
// 获取验证码
const getCaptCha = async () => {
  const res = await getCode()
  if (res.code === 200) {
    captchaUrl.value = res.data as string
  }
  console.log(res)
}
// 生命周期
onMounted(() => {
  getCaptCha()
})

// 手机号验证规则
const validateMobile = async (_rule: any, value: string) => {
  if (!value) {
    return Promise.reject(new Error('请输入手机号'))
  }
  const mobileReg = /^1[3-9]\d{9}$/
  if (!mobileReg.test(value)) {
    return Promise.reject(new Error('请输入正确的手机号'))
  }
  return Promise.resolve()
}

// 用户名验证规则
const validateUsername = async (_rule: any, value: string) => {
  if (!value) {
    return Promise.reject(new Error('请输入用户名'))
  }
  if (value.length < 3 || value.length > 16) {
    return Promise.reject(new Error('用户名长度为3-16个字符'))
  }
  return Promise.resolve()
}

// 验证码验证规则
const validateCode = async (_rule: any, value: string) => {
  if (!value) {
    return Promise.reject(new Error('请输入验证码'))
  }
  if (value.length !== 4) {
    return Promise.reject(new Error('验证码为4位验证码'))
  }
  return Promise.resolve()
}

// 表单验证规则
const rules = {
  mobile: [{ required: true, validator: validateMobile, trigger: 'blur' }],
  nickname: [{ required: true, validator: validateUsername, trigger: 'blur' }],
  captchaCode: [{ required: true, validator: validateCode, trigger: 'blur' }],
}

const formRef = ref()
const loading = ref(false)

// 提交注册
const handleRegister = async () => {
  try {
    await formRef.value.validate()
    loading.value = true

    console.log('注册信息:', formState)
    const res = await register(formState)

    if (res.code === 200) {
      message.success('注册成功!')
    }

    // 跳转到登录页或首页
    navigateTo('/login')
  } catch (error) {
    message.error('注册失败:' + error)
  } finally {
    loading.value = false
  }
}
</script>

<template>
  <div class="register-container">
    <div class="register-main-box">
      <!-- 左侧 Logo 信息区域 -->
      <div class="register-left">
        <div class="left-content">
          <div class="logo-section">
            <div class="logo-wrapper">
              <IconLogo class="logo-icon" />
            </div>
            <h1 class="welcome-title">鹿鸣博客</h1>
            <p class="welcome-subtitle">记录生活点滴，分享技术心得</p>
          </div>

          <div class="features-grid">
            <div class="feature-card">
              <div class="feature-icon">
                <svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                  <path
                    d="M9 12L11 14L15 10M21 12C21 16.9706 16.9706 21 12 21C7.02944 21 3 16.9706 3 12C3 7.02944 7.02944 3 12 3C16.9706 3 21 7.02944 21 12Z"
                    stroke="currentColor"
                    stroke-width="2"
                    stroke-linecap="round"
                    stroke-linejoin="round"
                  />
                </svg>
              </div>
              <div class="feature-content">
                <h3>技术分享</h3>
                <p>分享编程经验和技巧</p>
              </div>
            </div>

            <div class="feature-card">
              <div class="feature-icon">
                <svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                  <path
                    d="M13 2L3 14H12L11 22L21 10H12L13 2Z"
                    stroke="currentColor"
                    stroke-width="2"
                    stroke-linecap="round"
                    stroke-linejoin="round"
                  />
                </svg>
              </div>
              <div class="feature-content">
                <h3>创作自由</h3>
                <p>随心记录生活与思考</p>
              </div>
            </div>
          </div>
        </div>
      </div>

      <!-- 右侧注册表单区域 -->
      <div class="register-right">
        <div class="register-form-wrapper">
          <!-- 注册表单 -->
          <a-form
            ref="formRef"
            :model="formState"
            :rules="rules"
            layout="vertical"
            class="register-form"
          >
            <!-- 手机号 -->
            <a-form-item label="手机号" name="mobile">
              <a-input
                v-model:value="formState.mobile"
                size="large"
                placeholder="请输入手机号"
                :maxlength="11"
              >
                <template #prefix>
                  <svg
                    class="input-icon"
                    viewBox="0 0 24 24"
                    fill="none"
                    xmlns="http://www.w3.org/2000/svg"
                  >
                    <path
                      d="M5 4H9L11 9L8.5 10.5C9.57096 12.6715 11.3285 14.429 13.5 15.5L15 13L20 15V19C20 19.5304 19.7893 20.0391 19.4142 20.4142C19.0391 20.7893 18.5304 21 18 21C14.0993 20.763 10.4202 19.1065 7.65683 16.3432C4.8935 13.5798 3.23705 9.90074 3 6C3 5.46957 3.21071 4.96086 3.58579 4.58579C3.96086 4.21071 4.46957 4 5 4"
                      stroke="currentColor"
                      stroke-width="2"
                      stroke-linecap="round"
                      stroke-linejoin="round"
                    />
                  </svg>
                </template>
              </a-input>
            </a-form-item>

            <!-- 用户名 -->
            <a-form-item label="用户名" name="username">
              <a-input
                v-model:value="formState.nickname"
                size="large"
                placeholder="请输入用户名"
                :maxlength="16"
              >
                <template #prefix>
                  <svg
                    class="input-icon"
                    viewBox="0 0 24 24"
                    fill="none"
                    xmlns="http://www.w3.org/2000/svg"
                  >
                    <path
                      d="M20 21V19C20 17.9391 19.5786 16.9217 18.8284 16.1716C18.0783 15.4214 17.0609 15 16 15H8C6.93913 15 5.92172 15.4214 5.17157 16.1716C4.42143 16.9217 4 17.9391 4 19V21"
                      stroke="currentColor"
                      stroke-width="2"
                      stroke-linecap="round"
                      stroke-linejoin="round"
                    />
                    <path
                      d="M12 11C14.2091 11 16 9.20914 16 7C16 4.79086 14.2091 3 12 3C9.79086 3 8 4.79086 8 7C8 9.20914 9.79086 11 12 11Z"
                      stroke="currentColor"
                      stroke-width="2"
                      stroke-linecap="round"
                      stroke-linejoin="round"
                    />
                  </svg>
                </template>
              </a-input>
            </a-form-item>

            <!-- 验证码 -->
            <a-form-item label="验证码" name="captchaCode">
              <div class="code-input-group">
                <a-input
                  v-model:value="formState.captchaCode"
                  size="large"
                  placeholder="请输入验证码"
                  :maxlength="4"
                >
                  <template #prefix>
                    <svg
                      class="input-icon"
                      viewBox="0 0 24 24"
                      fill="none"
                      xmlns="http://www.w3.org/2000/svg"
                    >
                      <rect
                        x="3"
                        y="11"
                        width="18"
                        height="11"
                        rx="2"
                        ry="2"
                        stroke="currentColor"
                        stroke-width="2"
                        stroke-linecap="round"
                        stroke-linejoin="round"
                      />
                      <path
                        d="M7 11V7C7 5.67392 7.52678 4.40215 8.46447 3.46447C9.40215 2.52678 10.6739 2 12 2C13.3261 2 14.5979 2.52678 15.5355 3.46447C16.4732 4.40215 17 5.67392 17 7V11"
                        stroke="currentColor"
                        stroke-width="2"
                        stroke-linecap="round"
                        stroke-linejoin="round"
                      />
                    </svg>
                  </template>
                </a-input>
                <a-button size="large" class="send-code-btn" @click="getCaptCha">
                  <img :src="captchaUrl" />
                </a-button>
              </div>
            </a-form-item>

            <!-- 提交按钮 -->
            <a-form-item>
              <a-space direction="vertical" style="width: 100%" :size="16">
                <a-button
                  type="primary"
                  size="large"
                  block
                  :loading="loading"
                  class="submit-btn"
                  @click="handleRegister"
                >
                  立即注册
                </a-button>
              </a-space>
            </a-form-item>
          </a-form>

          <!-- 底部链接 -->
          <div class="register-footer">
            <span>已有账户？</span>
            <NuxtLink to="/login" class="login-link">立即登录</NuxtLink>
          </div>
        </div>
      </div>
    </div>

    <!-- 背景装饰 -->
    <!-- <div class="bg-decoration">
      <div class="circle circle-1" />
      <div class="circle circle-2" />
      <div class="circle circle-3" />
      <div class="floating-element element-1" />
      <div class="floating-element element-2" />
      <div class="floating-element element-3" />
    </div> -->
  </div>
</template>

<style scoped lang="less">
.register-container {
  width: 100%;
  height: calc(100vh - 140px);
  display: flex;
  align-items: center;
  justify-content: center;
  background: white;
  padding: 20px;
  position: relative;
  overflow: hidden;
}

.register-main-box {
  width: 100%;
  max-width: 1000px;
  background: white;
  border-radius: 24px;
  box-shadow: none;
  position: relative;
  z-index: 1;
  display: flex;
  animation: none;
  overflow: hidden;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.08);
}

/* 左侧 Logo 区域 */
.register-left {
  flex: 1;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  position: relative;
  overflow: hidden;
  box-shadow: none;
}

.left-content {
  max-width: 400px;
  width: 100%;
  position: relative;
  z-index: 2;
}

.logo-section {
  text-align: center;
  margin-bottom: 40px;
}

.logo-wrapper {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 90px;
  height: 90px;
  background: rgba(255, 255, 255, 0.15);
  border-radius: 24px;
  margin-bottom: 24px;
  backdrop-filter: blur(10px);
  box-shadow: none;
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.logo-icon {
  width: 50px;
  height: 50px;
  color: white;
}

.welcome-title {
  font-size: 32px;
  font-weight: 700;
  margin: 0 0 12px 0;
  background: rgba(255, 255, 255, 0.95);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.welcome-subtitle {
  font-size: 16px;
  color: rgba(255, 255, 255, 0.9);
  margin: 0 0 0 0;
  line-height: 1.6;
}

.features-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
  margin-top: 30px;
}

.feature-card {
  background: rgba(255, 255, 255, 0.1);
  border-radius: 16px;
  padding: 20px;
  backdrop-filter: blur(5px);
  border: 1px solid rgba(255, 255, 255, 0.15);
  box-shadow: none;
}

.feature-card:hover {
  background: rgba(255, 255, 255, 0.15);
  box-shadow: none;
}

.feature-icon {
  width: 44px;
  height: 44px;
  min-width: 44px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  backdrop-filter: blur(5px);
  margin-bottom: 16px;
}

.feature-icon svg {
  width: 22px;
  height: 22px;
  color: white;
}

.feature-content h3 {
  font-size: 16px;
  font-weight: 600;
  margin: 0 0 6px 0;
  color: white;
}

.feature-content p {
  font-size: 13px;
  color: rgba(255, 255, 255, 0.85);
  margin: 0;
  line-height: 1.5;
}

/* 右侧表单区域 */
.register-right {
  flex: 1;
  padding: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: white;
  box-shadow: none;
}

.register-form-wrapper {
  width: 100%;
  max-width: 380px;
}

.form-header {
  text-align: center;
  margin-bottom: 32px;
}

.form-header h2 {
  font-size: 28px;
  font-weight: 700;
  color: #1a202c;
  margin: 0 0 8px 0;
}

.form-header p {
  font-size: 15px;
  color: #718096;
  margin: 0;
}

.register-form {
  margin-top: 16px;
}

:deep(.ant-form-item-label > label) {
  font-size: 14px;
  font-weight: 600;
  color: #2d3748;
}

:deep(.ant-input-affix-wrapper) {
  border-radius: 12px;
  border: 2px solid #e2e8f0;
  transition: all 0.3s;
  height: 48px;
  box-shadow:
    0 1px 3px 0 rgba(0, 0, 0, 0.05),
    0 1px 2px 0 rgba(0, 0, 0, 0.03);
}

:deep(.ant-input-affix-wrapper:hover),
:deep(.ant-input-affix-wrapper-focused) {
  border-color: #667eea;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

:deep(.ant-input) {
  font-size: 15px;
  color: #2d3748;
}

:deep(.ant-input::placeholder) {
  color: #a0aec0;
}

.input-icon {
  width: 20px;
  height: 20px;
  color: #667eea;
}

.code-input-group {
  display: flex;
  gap: 12px;
}

.code-input-group :deep(.ant-input-affix-wrapper) {
  flex: 1;
}

.send-code-btn {
  overflow: hidden;
  width: 144px;
  border-radius: 12px;
  border: 2px solid #667eea;
  padding: 0;
  color: #667eea;
  font-weight: 600;
  height: 48px;
  box-shadow:
    0 1px 3px 0 rgba(0, 0, 0, 0.05),
    0 1px 2px 0 rgba(0, 0, 0, 0.03);
  img {
    transform: scale(1.2);
  }
}

.submit-btn {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  border-radius: 12px;
  height: 48px;
  font-size: 16px;
  font-weight: 600;
  box-shadow:
    0 4px 12px rgba(102, 126, 234, 0.4),
    0 2px 4px rgba(102, 126, 234, 0.2);
  transition: all 0.3s;
}

.submit-btn:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 8px 20px rgba(102, 126, 234, 0.5);
}

.reset-btn {
  border-radius: 12px;
  height: 48px;
  font-size: 16px;
  font-weight: 600;
  border: 2px solid #e2e8f0;
  color: #4a5568;
  transition: all 0.3s;
  background: transparent;
  box-shadow:
    0 1px 3px 0 rgba(0, 0, 0, 0.05),
    0 1px 2px 0 rgba(0, 0, 0, 0.03);
}

.reset-btn:hover {
  border-color: #cbd5e0;
  color: #2d3748;
  background: #f8fafc;
}

.register-footer {
  text-align: center;
  margin-top: 24px;
  padding-top: 24px;
  border-top: 1px solid #e2e8f0;
  font-size: 14px;
  color: #718096;
}

.login-link {
  color: #667eea;
  font-weight: 600;
  text-decoration: none;
  margin-left: 4px;
  transition: all 0.3s;
  position: relative;
}

.login-link:hover {
  color: #764ba2;
  text-decoration: none;
}

.login-link::after {
  content: '';
  position: absolute;
  bottom: -2px;
  left: 0;
  width: 0;
  height: 2px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  transition: width 0.3s ease;
}

.login-link:hover::after {
  width: 100%;
}

/* 背景装饰 */
.bg-decoration {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  overflow: hidden;
  z-index: 0;
}

.circle {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  animation: float 20s infinite ease-in-out;
}

.circle-1 {
  width: 300px;
  height: 300px;
  top: -100px;
  left: -100px;
  animation-delay: 0s;
}

.circle-2 {
  width: 200px;
  height: 200px;
  bottom: -50px;
  right: -50px;
  animation-delay: 2s;
}

.circle-3 {
  width: 150px;
  height: 150px;
  top: 50%;
  right: 10%;
  animation-delay: 4s;
}

.floating-element {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.05);
  animation: float 15s infinite ease-in-out;
}

.element-1 {
  width: 80px;
  height: 80px;
  top: 20%;
  right: 20%;
  animation-delay: 1s;
}

.element-2 {
  width: 60px;
  height: 60px;
  bottom: 30%;
  left: 15%;
  animation-delay: 3s;
}

.element-3 {
  width: 40px;
  height: 40px;
  top: 60%;
  left: 25%;
  animation-delay: 5s;
}

@keyframes float {
  0%,
  100% {
    transform: translateY(0) rotate(0deg);
  }
  50% {
    transform: translateY(-30px) rotate(180deg);
  }
}

/* 响应式 */
@media (max-width: 992px) {
  .register-main-box {
    flex-direction: column;
    max-width: 550px;
  }

  .register-left,
  .register-right {
    width: 100%;
  }

  .register-left {
    padding: 40px 32px;
  }

  .register-right {
    padding: 40px 32px;
  }

  .welcome-title {
    font-size: 28px;
  }

  .features-grid {
    grid-template-columns: 1fr;
    gap: 16px;
  }

  .feature-card {
    padding: 16px;
  }
}

@media (max-width: 640px) {
  .register-container {
    padding: 10px;
  }

  .register-main-box {
    border-radius: 20px;
  }

  .register-left,
  .register-right {
    padding: 32px 24px;
  }

  .welcome-title {
    font-size: 24px;
  }

  .form-header h2 {
    font-size: 24px;
  }

  .code-input-group {
    flex-direction: column;
  }

  .send-code-btn {
    width: 100%;
  }

  .features-grid {
    gap: 12px;
  }
}

/* 表单验证错误样式优化 */
:deep(.ant-form-item-has-error .ant-input-affix-wrapper) {
  border-color: #f56565;
}

:deep(.ant-form-item-has-error .ant-input-affix-wrapper:focus) {
  box-shadow: 0 0 0 3px rgba(245, 101, 101, 0.1);
}

:deep(.ant-form-item-explain-error) {
  font-size: 13px;
  margin-top: 4px;
}
</style>
